<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link href="CSS/default.css" rel="stylesheet" type="text/css" />
		<link rel="stylesheet" type="text/css" href="Themes/default/easyui.css" />
		<link rel="stylesheet" type="text/css" href="CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="Themes/icon.css" />
		<style type="text/css">
			table {
				margin-bottom: 10px;
			}
			
			table tr {
				margin-top: 20px;
				margin-bottom: 20px;
			}
		</style>
	</head>

	<body>
		<div style="width: 800px">
			<div class="easyui-panel" title="数据查询" style="width:100%;padding:10px;">
				<table width="100%">
					<tr>
						<td width="10%">查询用户名</td>
						<td width="40%"><input id="input_searchUser" class="easyui-textbox" style="width:100%;" /></td>
						<td width="10%">查询组织</td>
						<td width="40%"><input id="input_searchOU" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
					<tr>
						<td>属性名称</td>
						<td><input id="input_searchPropertyName" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
				</table>
				<div id="div_button">
					<a href="#" class="easyui-linkbutton" onclick="getUser()">查询用户数据</a>
					<a href="#" class="easyui-linkbutton" onclick="getOU()">获取组织数据</a>
					<a href="#" class="easyui-linkbutton" onclick="getUserProperty()">查询用户属性</a>
				</div>
			</div>

			<div class="easyui-panel" title="数据创建" style="width:100%;padding:10px;">
				<table width="100%">
					<tr>
						<td width="30%">组织机构(示例 OU1/OU2/OU3)</td>
						<td width="70%"><input id="input_createOU" class="easyui-textbox" style="width: 100%;" /></td>
					</tr>
					<tr>
						<td>创建用户(示例 OU1/OU2/username)</td>
						<td><input id="input_createUser" class="easyui-textbox" style="width: 100%;" /></td>
					</tr>
				</table>
				<div id="div_button">
					<a href="#" class="easyui-linkbutton" onclick="createUser()">创建用户</a>
					<a href="#" class="easyui-linkbutton" onclick="enabledUser()">启用用户</a>
					<a href="#" class="easyui-linkbutton" onclick="createOU()">创建组织</a>
					<a href="#" class="easyui-linkbutton" onclick="deleteUser()">删除用户</a>
					<a href="#" class="easyui-linkbutton" onclick="deleteOU()">删除组织</a>
				</div>
			</div>

			<div class="easyui-panel" title="密码修改" style="width:100%;padding:10px;">
				<table width="100%">
					<tr>
						<td width="10%">用户名(示例 username)</td>
						<td width="40%"><input id="input_modiPassWordUser" class="easyui-textbox" style="width:100%;" /></td>
						<td width="10%">密码</td>
						<td width="40%"><input id="input_modiPassWord" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
				</table>
				<div id="div_button">
					<a href="#" class="easyui-linkbutton" onclick="changePassword()">修改密码</a>

				</div>
			</div>

			<div class="easyui-panel" title="属性修改" style="width:100%;padding:10px;">
				<table width="100%">
					<tr>
						<td width="10%">用户名(username)</td>
						<td width="40%"><input id="input_modiPropertyUser" class="easyui-textbox" style="width:100%;" /></td>
						<td width="10%">属性名称</td>
						<td width="40%"><input id="input_modiProperty" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
					<tr>
						<td>属性值</td>
						<td><input id="input_modiPropertyValue" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
				</table>
				<div id="div_button">
					<a href="#" class="easyui-linkbutton" onclick="setProperty()">修改属性值</a>
				</div>
			</div>
			<div class="easyui-panel" title="组织修改" style="width:100%;padding:10px;">
				<table width="100%">
					<tr>
						<td width="10%">原组织名(示例： OU1/OU2/OU3)</td>
						<td width="40%"><input id="input_modiOldOU" class="easyui-textbox" style="width:100%;" /></td>
						<td width="10%">新组织名(示例：newOU)</td>
						<td width="40%"><input id="input_modiNewOU" class="easyui-textbox" style="width:100%;" /></td>
					</tr>
				</table>
				<div id="div_button">
					<a href="#" class="easyui-linkbutton" onclick="setOUName()">修改组织名</a>
				</div>
			</div>
			<div style="margin-top: 10px; width: 100%;">
				<span> 返回结果</span>
				<input id="input_res" class="easyui-textbox" label="返回结果:" labelPosition="top" multiline="true" data-options="readonly:true" style="width:100%;height:120px">
			</div>
		</div>
	</body>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="js/json2.js"></script>
	<script type="text/javascript">
		function getUser() {
			var username = $("#input_searchUser").val();
			var sendData = new Object();
			sendData.sUserName = username;
			PostToServer("WebGetUser", sendData);
		}

		function getOU() {
			var ouname = $("#input_searchOU").val();
			var sendData = new Object();
			var s = constructParent(ouname);
			sendData.sOUName = s.name;
			sendData.sParentLocation = s.parent;
			PostToServer("WebGetOU", sendData);
		}

		function createUser() {
			var username = $("#input_createUser").val();
			var sendData = new Object();
			var s = constructParent(username);
			sendData.sCN = s.name;
			sendData.sParentLocation = s.parent;
			PostToServer("WebCreateNewUser", sendData);

		}

		function createOU() {
			var ouname = $("#input_createOU").val();
			var sendData = new Object();
			var s = constructParent(ouname);
			sendData.sOUName = s.name;
			sendData.sParentLocation = s.parent;
			PostToServer("WebCreateOU", sendData);
		}

		function changePassword() {
			var username = $("#input_modiPassWordUser").val();
			var pas = $("#input_modiPassWord").val();
			var sendData = new Object();
			sendData.sUserName = username;
			sendData.sNewPassword = pas;
			PostToServer("WebSetPassWord", sendData);
		}
		
		function enabledUser()
		{
			var username = $("#input_createUser").val();
			var sendData = new Object();
			var s = constructParent(username);
			sendData.sUserName = s.name;
			PostToServer("WebEnableUserAccount", sendData);
		}

		function deleteUser() {
			var username = $("#input_createUser").val();
			var sendData = new Object();
			var s = constructParent(username);
			sendData.sUserName = s.name;
			PostToServer("WebDeleteUser", sendData);
		}

		function deleteOU() {
            var ouname = $("#input_createOU").val();
			var sendData = new Object();
			var s = constructParent(ouname);
			sendData.sOUName = s.name;
			sendData.sParentLocation = s.parent;
			PostToServer("WebDeleteOU", sendData);
		}
//      修改名称属性为sAMAccountName
		function setProperty() {
			var username = $("#input_modiPropertyUser").val();
			var propertyname = $("#input_modiProperty").val();
			var propertyvalue = $("#input_modiPropertyValue").val();
			var sendData = new Object();
			sendData.sUserName = username;
			sendData.sPropertyName = propertyname;
			sendData.sPropertyValue = propertyvalue;
			PostToServer("WebSetProperty", sendData);
		}
		
		function setOUName()
		{
			var oldname = $("#input_modiOldOU").val();
			var newname = $("#input_modiNewOU").val();
			var sendData = new Object();
			var s = constructParent(oldname);
			sendData.sOldOUName = s.name;
			sendData.sNewOUName=newname;
			sendData.sParentLocation = s.parent;
			PostToServer("WebSetOUName", sendData);
		}

		function getUserProperty() {
			var username = $("#input_searchUser").val();
			var propertyname = $("#input_searchPropertyName").val();
			var sendData = new Object();
			sendData.sUserName = username;
			sendData.sPropertyName = propertyname;
			PostToServer("WebGetUserProperty", sendData);

		}

		function PostToServer(key, senddata) {
			_url = "http://localhost:53748/WebService1.asmx/" + key;
			var postData = JSON.stringify(senddata);
			$.ajax({
				type: 'POST',
				url: _url,
				data: postData,
				dataType: 'json',
				contentType: "application/json",
				success: function(data) {
					$("#input_res").textbox("setValue", data.d);
				}
			});
		}

		function constructParent(value) {
			var res = new Object();
			res.parent = "";
			res.name = "";
			console.log(value);
			var m = value.split("/");
			console.log(value);
			for(var i = 0; i < m.length; i++) {
				if(i == m.length - 1) {
					res.name = m[i];
				} else {
					res.parent = m[i] + "/" + res.parent;
				}
			}
			return res;

		}
	</script>

</html>